//
//  RequestManage.m
//  citzenWorkStation
//
//  Created by yang qing on 2018/2/24.
//  Copyright © 2018年 yang qing. All rights reserved.
//

#import "RequestManage.h"
#import "AFNetworking.h"
@interface RequestManage()
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end
@implementation RequestManage
- (AFHTTPSessionManager *)manager{
    if(!_manager){
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        _manager.responseSerializer.acceptableContentTypes = [_manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/plain", @"text/html", nil]];
        _manager.securityPolicy = [AFSecurityPolicy defaultPolicy];
        _manager.securityPolicy.allowInvalidCertificates = YES;
        _manager.securityPolicy.validatesDomainName = NO;
    }
    return _manager;
}

 
- (void)requestWithMethod:(HttpRequestMethod)httpRequestMethod
                      url:(NSString *)url
                    param:(id)params
                  success:(SuccessBlock)successBlock
                  failure:(FailureBlock)failureBlock{
    if(httpRequestMethod == HttpRequestMethodGet){
        [self.manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if(responseObject){
                successBlock(responseObject);
            }else{
                failureBlock(nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failureBlock(error);
        }];
    }else if(httpRequestMethod == HttpRequestMethodPost){
        [self.manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if(responseObject){
                successBlock(responseObject);
            }else{
                failureBlock(nil);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failureBlock(error);
        }];
    }
}

- (void)uploadFileMethod:(NSString *)url
                   param:(NSDictionary *)params
                    file:(id)data
                fileName:(NSString *)fileName
                fileType:(NSString *)fileType
                progress:(ProgressBlock)progressBlock
                 success:(SuccessBlock)successBlock
                 failure:(FailureBlock)failureBlock{
    [self.manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if(data){
            [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:fileType];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
        if(progressBlock){
            progressBlock(100 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
        }
        NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(responseObject){
            successBlock(responseObject);
        }else{
            failureBlock(nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock(error);
    }];
}
@end
